import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author  8100556811
 */
public class MenuTest extends Race implements ActionListener
{
	
	public static void main(String[] args) {
		new MenuTest().run();
	}

	/**
	 * @uml.property  name="configAction"
	 * @uml.associationEnd  
	 */
	protected GameAction configAction;
	
	private JButton playButton;
	private JButton configButton;
	private JButton quitButton;
	private JButton pauseButton;
	private JPanel	playButtonSpace;
	
	

	
	public void init()
	{
		super.init();
		//avoid component to repaint itself
		NullRepaintManager.install();
		
		configAction = new GameAction("config");
		
		//create buttons
		quitButton = createButton("quit", "Quit");
		playButton = createButton("play", "Start/Resume");
		pauseButton = createButton("pause", "Pause");
		configButton = createButton("config", "New Race");
		
		//create the space to place the playbutton
		playButtonSpace = new JPanel();
		playButtonSpace.setOpaque(false);
		playButtonSpace.add(playButton);
		
		JFrame frame = super.screen.getFullScreenWindow();
		Container contentPane = frame.getContentPane();

		//check transparency
		if (contentPane instanceof JComponent)
		{
			((JComponent)contentPane).setOpaque(false);
		}

		//add components to the pane
		contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
		contentPane.add(playButtonSpace);
		contentPane.add(configButton);
		contentPane.add(quitButton);
		
		//explicitly layout components
		frame.validate();
		
	}
	
	
	public void draw (Graphics2D g)
	{
		super.draw(g);
		JFrame frame = super.screen.getFullScreenWindow();
		
		//the layered pane contains
		frame.getLayeredPane().paintComponents(g);
		
	}
	
	public void setPaused(boolean p)
	{
		super.setPaused(p);
		playButtonSpace.removeAll();
		if (isPaused())
		{
			playButtonSpace.add(playButton);
		}
		else{
			playButtonSpace.add(pauseButton);
			}				
	}
	
	//call awt thread when a button is pressed
	public void actionPerformed (ActionEvent e)
	{
		Object src = e.getSource();
		if (src== quitButton)
		{
			super.exit.tap();
		}
		else if(src== configButton)
		{
		//	super.gameLoop();
			
		}
		else if(src== playButton || src== pauseButton)
		{
			super.pause.tap();
		}
	}
	
	
	//creates swing jbutton using images
	
	public JButton createButton(String name, String toolTip)
	{
		//load image
		String imagePath = "images/menu/" + name +".png";
		ImageIcon iconRollover = new ImageIcon(imagePath);
		int w = iconRollover.getIconWidth();
		int h = iconRollover.getIconHeight();
		
		//get cursor
		Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
		
		//make translucent default img
		Image image = screen.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
		Graphics2D g = (Graphics2D)image.getGraphics();
		Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
		g.setComposite(alpha);
		g.drawImage(iconRollover.getImage(),0,0,null);
		g.dispose();
		ImageIcon iconDefault = new ImageIcon(image);
		
		
		//make a pressed image
		image = screen.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
		g = (Graphics2D)image.getGraphics();
		g.drawImage(iconRollover.getImage(),2,2,null);
		g.dispose();
		ImageIcon iconPressed = new ImageIcon(image);
		
		//create Button
		JButton button = new JButton();
		
		button.addActionListener(this);
		button.setIgnoreRepaint(true);
		button.setFocusable(false);
		button.setToolTipText(toolTip);
		button.setBorder(null);
		button.setContentAreaFilled(false);
		button.setCursor(cursor);
		button.setIcon(iconDefault);
		button.setRolloverIcon(iconRollover);
		button.setPressedIcon(iconPressed);
		
		return button;
	}
	
	
	
	
	
	
	
	
	
	
	
}
